# -*- coding: UTF-8 -*-
import os
import sys
import datetime
import time
import re

reload(sys)
sys.setdefaultencoding('utf-8')


class Converter(object):
    """
    转换类，包括转成罗马音和汉字注音
    汉字注音不完善，有些音无法用汉字普通话标注，采用通用汉语拼音
    引入了拗音的处理
    """

    @classmethod
    def chinese_to_kana(cls, initial_content):
        """
        将原始文本中的日语汉字转换为假名，
        使整个文本以假名形式存在，由于汉字假名对照表的不完整，有些不常用汉字将不被转换
        :param initial_content:一行文本
        :return:以假名形式存在的文本
        """

        chinese_kana_file = open(u'汉字注音dict.txt', 'r')
        ch_to_kana_dict = {}
        for line in chinese_kana_file:
            ch = unicode(line).split(' ')[0].strip('﻿')
            kana = unicode(line).split(' ')[1].strip()
            ch_to_kana_dict[ch] = kana
        chinese_kana_file.close()
        trans_res = ''
        for word in unicode(initial_content):
            if ch_to_kana_dict.get(word):
                trans_res += ch_to_kana_dict.get(word)
            else:
                trans_res += word

        return trans_res

    @classmethod
    def jp_to_ch(cls, initial_content):
        """
        日语文本转换为汉字注音
        :param initial_content:
        :return:以汉字注音的文本
        """
        initial_content = unicode(initial_content)
        kana_file = open(u'假名读音.txt', 'r')
        kana_ch_dict = {}
        for line in kana_file:
            kana = unicode(line).split('\t')[0].strip('﻿')
            ch = unicode(line).split('\t')[2].strip()
            kana_ch_dict[kana] = ch
        kana_file.close()
        # 先处理拗音产生的连读假名，两个两个扫
        init_len = len(unicode(initial_content))
        trans_res = ''
        cur = 0
        while cur < init_len:
            # 两个两个处理
            if cur == init_len - 1:
                trans_res += initial_content[cur]
            else:
                word_couple = initial_content[cur] + initial_content[cur + 1]
                if kana_ch_dict.get(word_couple):
                    trans_res += kana_ch_dict.get(word_couple)
                    cur += 1
                elif kana_ch_dict.get(initial_content[cur]):
                    trans_res += kana_ch_dict.get(initial_content[cur])
                else:
                    trans_res += initial_content[cur]
            cur += 1
        return trans_res

    @classmethod
    def jp_to_rome(cls, initial_content):
        """
        日语文本转换为罗马音标注
        :param initial_content:
        :return:以罗马音注音的文本
        """
        initial_content = unicode(initial_content)
        kana_file = open(u'假名读音.txt', 'r')
        kana_rome_dict = {}
        for line in kana_file:
            kana = unicode(line).split('\t')[0].strip('﻿')
            rome = unicode(line).split('\t')[1].strip()
            kana_rome_dict[kana] = rome
        kana_file.close()
        # 先处理拗音产生的连读假名，两个两个扫
        init_len = len(unicode(initial_content))
        trans_res = ''
        cur = 0
        while cur < init_len:
            # 两个两个处理
            if cur == init_len - 1:
                trans_res += initial_content[cur]
            else:
                word_couple = initial_content[cur] + initial_content[cur + 1]
                if kana_rome_dict.get(word_couple):
                    trans_res += kana_rome_dict.get(word_couple) + ' '
                    cur += 1
                elif kana_rome_dict.get(initial_content[cur]):
                    trans_res += kana_rome_dict.get(initial_content[cur]) + ' '
                else:
                    trans_res += initial_content[cur]
            cur += 1
        return trans_res


if __name__ == "__main__":
    butterfly = open(u'日语文章.txt', 'r')
    for line in butterfly:
        print Converter.jp_to_ch(Converter.chinese_to_kana(unicode(line)))

